import { createRouter, createWebHistory } from 'vue-router'
import { extractRoutes } from '../config/menuConfig.js'

// 从统一配置中提取路由配置
const routes = extractRoutes()

const router = createRouter({
  history: createWebHistory(),
  routes,
  // 修复路由模式，确保组件正确切换
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})
// router.afterEach((to, from) => {
//   console.log('afterEach', to, from)
// })
// 简化的路由守卫，确保路由能正确跳转
router.beforeEach((to, from, next) => {
  console.log('路由守卫执行:', from.path, '->', to.path)
  
  // 检查是否已登录，如果未登录则跳转到登录页
  const isLoggedIn = localStorage.getItem('isLoggedIn')
  if (to.path !== '/login' && to.path !== '/' && !isLoggedIn) {
    console.log('未登录，跳转到登录页')
    next('/login')
    return
  }
  
  // 直接允许所有导航
  console.log('路由守卫允许跳转:', to.path)
  next()
})

export default router